package server;

import servlet.HttpServlet;

import java.net.Socket;
import java.util.Map;

/**
 * 线程类 实现多线程
 */
public class RequestProcessor extends  Thread{

    private Socket socket;

    private Map<String,HttpServlet> servletMap;

    public RequestProcessor(Socket socket, Map servletMap){
        this.socket = socket;
        this.servletMap = servletMap;
    }


    @Override
    public void run(){
        try {
            Request request = new Request(socket.getInputStream());
            String url = request.getUrl();
            Response response = new Response(socket.getOutputStream());
            if (servletMap.get(url)==null){//静态资源处理
                response.outputHtml(url);
            }else{//动态资源servlet处理
                HttpServlet httpServlet = servletMap.get(url);
                httpServlet.service(request,response);
            }
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

